package com.example.activity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.example.model.HistoryModel;
import com.example.test.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class LaunchActivity extends Activity {
	private HistoryModel hisModel;
	private Cursor mCursor;


	private LocationManager locationManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.launch);
		
		hisModel=new HistoryModel(this);
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		updateView(location);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				2000, 8, new LocationListener() {

					@Override
					public void onLocationChanged(Location location) {
						updateView(location);

					}

					@Override
					public void onProviderDisabled(String provider) {

						updateView(null);
					}

					@Override
					public void onProviderEnabled(String provider) {
						updateView(locationManager
								.getLastKnownLocation(provider));

					}

					@Override
					public void onStatusChanged(String provider, int status,
							Bundle extras) {
						// TODO Auto-generated method stub

					}

				});
		
		
	}
	private void updateView(Location location) {
		if (location != null) {
			
			
			double lat=location.getLatitude();
			double lon=location.getLongitude();
			DecimalFormat df = new DecimalFormat("#.0");
			String strLat=df.format(lat);
			String strLon=df.format(lon);
			String currentLocation=strLat+","+strLon;
			
			Calendar c = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String time = sdf.format(c.getTime());

			SimpleDateFormat tf = new SimpleDateFormat("H");
			String timef = tf.format(c.getTime());
			int t = Integer.parseInt(timef);
			String timeFrame = null;
			if (t >= 6 && t <= 8)

				timeFrame = "Morning";

			else if (t >= 9 && t < 11)
				timeFrame = "Antemeridiem";
			else if (t >= 11 && t < 13)
				timeFrame = "LunchTime";
			else if (t >= 13 && t < 17)
				timeFrame = "Afternoon";
			else if (t >= 17 && t < 19)
				timeFrame = "DinnerTime";
			else if (t >= 19 && t < 23)
				timeFrame = "Evening";
			else
				timeFrame = "Midnight";
						
			Toast.makeText(this, "You are at:"+currentLocation, Toast.LENGTH_LONG).show();
			
			boolean urlb=false;
			urlb=query(currentLocation);
			if(urlb==true)
			{
			
			Intent gpsloc=new Intent(LaunchActivity.this,FavoriteView.class);
			Bundle bundle=new Bundle();
			bundle.putString("CURLOC", currentLocation);
			bundle.putString("TIMEFRAME",timeFrame);
			gpsloc.putExtras(bundle);
			startActivity(gpsloc);
			}
			else
			{
				Intent intent = new Intent(LaunchActivity.this,
						BrowserView.class);
				Bundle bundle = new Bundle();
				bundle.putString("CURLOC", currentLocation);
				
				intent.putExtras(bundle);
				startActivity(intent);
			}
			
		} else
		{
			Toast.makeText(this, "GPS doesn't work!!Set Location Information By Youself!",Toast.LENGTH_LONG).show();
			Intent setloc=new Intent(LaunchActivity.this,MapView.class);
			startActivity(setloc);
		}
			
	}
	
	public boolean query(String currentLocation) {
		String curLoc = currentLocation;

		Calendar c = Calendar.getInstance();
		SimpleDateFormat tf = new SimpleDateFormat("H");
		String timef = tf.format(c.getTime());
		int t = Integer.parseInt(timef);
		String timeFrame = null;
		if (t >= 6 && t <= 8)

			timeFrame = "Morning";

		  else if (t >= 9 && t < 11)
			timeFrame = "Antemeridiem";
		  else if (t >= 11 && t < 13)
			timeFrame = "LunchTime";
		  else if (t >= 13 && t < 17)
			timeFrame = "Afternoon";
		  else if (t >= 17 && t < 19)
			timeFrame = "DinnerTime";
		  else if (t >= 1 && t < 23)
			timeFrame = "Evening";
		else
			timeFrame = "Midnight";

		
		String url=hisModel.query_url(timeFrame, curLoc);
		System.out.printf("^^^^^^^^url",url);
		if(url==null)
		{
			Toast.makeText(this, "nonono!!", Toast.LENGTH_SHORT);
			return false;
		}
		else
		{
			Toast.makeText(this, "^^^^"+url, Toast.LENGTH_SHORT).show();
			return true;
		}
	}

}
